package com.project.movie.controller.like;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.movie.common.enums.BusinessType;
import com.project.movie.common.enums.ResultCode;
import com.project.movie.config.utils.ShiroUtils;
import com.project.movie.domain.*;
import com.project.movie.service.MovieItemService;
import com.project.movie.service.MovieLikeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

/**
 * @author 超级管理员
 * @version 1.0
 * @description: 想看controller
 * @date 2025/02/07 09:02
 */
@Controller
@ResponseBody
@RequestMapping("like")
public class MovieLikeController {

    @Autowired
    private MovieLikeService movieLikeService;
    @Autowired
    private MovieItemService movieItemService;

    /** 分页获取想看 */
    @PostMapping("getMovieLikePage")
    public Result getMovieLikePage(@RequestBody MovieLike movieLike) {
        User user = ShiroUtils.getUserInfo();
        movieLike.setUserId(user.getId());
        Page<MovieLike> page = new Page<>(movieLike.getPageNumber(),movieLike.getPageSize());
        QueryWrapper<MovieLike> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(StringUtils.isNotBlank(movieLike.getItemId()),MovieLike::getItemId,movieLike.getItemId())
                .eq(StringUtils.isNotBlank(movieLike.getUserId()),MovieLike::getUserId,movieLike.getUserId())
                .eq(StringUtils.isNotBlank(movieLike.getCreateBy()),MovieLike::getCreateBy,movieLike.getCreateBy())
                .eq(movieLike.getCreateTime() != null,MovieLike::getCreateTime,movieLike.getCreateTime())
                .eq(StringUtils.isNotBlank(movieLike.getUpdateBy()),MovieLike::getUpdateBy,movieLike.getUpdateBy())
                .eq(movieLike.getUpdateTime() != null,MovieLike::getUpdateTime,movieLike.getUpdateTime());
        Page<MovieLike> movieLikePage = movieLikeService.page(page, queryWrapper);
        for (MovieLike like : movieLikePage.getRecords()) {
            MovieItem movieItem = movieItemService.getById(like.getItemId());
            like.setCover(movieItem.getCover());
            like.setName(movieItem.getName());
            like.setLikeNum(movieItem.getLikeNum());
            like.setScore(movieItem.getScore());
        }
        return Result.success(movieLikePage);
    }

    /** 根据id获取想看 */
    @GetMapping("getMovieLikeById")
    public Result getMovieLikeById(@RequestParam("id")String id) {
        MovieLike movieLike = movieLikeService.getById(id);
        return Result.success(movieLike);
    }

    /** 保存想看 */
    @PostMapping("saveMovieLike")
    @Transactional(rollbackFor = Exception.class)
    public Result saveMovieLike(@RequestBody MovieLike movieLike) {
        User userInfo = ShiroUtils.getUserInfo();
        QueryWrapper<MovieLike> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(MovieLike::getItemId,movieLike.getItemId())
                .eq(MovieLike::getUserId,userInfo.getId());
        int count = movieLikeService.count(queryWrapper);
        if (count > 0) {
            return Result.fail("您已想看过该电影");
        }
        movieLike.setUserId(userInfo.getId());
        MovieItem movieItem = movieItemService.getById(movieLike.getItemId());
        movieItem.setLikeNum(movieItem.getLikeNum() + 1);
        movieItemService.updateById(movieItem);
        boolean save = movieLikeService.save(movieLike);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 编辑想看 */
    @PostMapping("editMovieLike")
    public Result editMovieLike(@RequestBody MovieLike movieLike) {
        boolean save = movieLikeService.updateById(movieLike);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 删除想看 */
    @GetMapping("removeMovieLike")
    @Transactional(rollbackFor = Exception.class)
    public Result removeMovieLike(@RequestParam("ids")String ids) {
        if (StringUtils.isNotBlank(ids)) {
            String[] asList = ids.split(",");
            for (String id : asList) {
                MovieLike movieLike = movieLikeService.getById(id);
                MovieItem movieItem = movieItemService.getById(movieLike.getItemId());
                movieItem.setLikeNum(movieItem.getLikeNum() - 1);
                movieItemService.updateById(movieItem);
                movieLikeService.removeById(id);
            }
            return Result.success();
        } else {
            return Result.fail("想看id不能为空！");
        }
    }

}
